<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增活动通知</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/css/global.css" media="all">
    <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/table.css" />
    <script type="text/javascript" src="/plugins/layui/layui.js"></script>
    <script type="text/javascript" src="/js/global-config.js"></script>
    <script charset="utf-8" src="/plugins/kindeditor/kindeditor-all.js"></script>
    <script charset="utf-8" src="/plugins/kindeditor/lang/zh-CN.js"></script>
    <script charset="utf-8" src="/plugins/kindeditor/plugins/code/prettify.js"></script>
    <script charset="utf-8" src="/js/kindEditor.js"></script>
    <style>
        .layui-form-label {
            width: 10%;
        }

        .layui-input-block {
            margin-left: 13%;
        }
    </style>
</head>
<body>
<div style="margin: 15px;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新增活动通知(<label class="layui-required-tip">标红的为必填项</label>)</legend>
    </fieldset>
    <form class="layui-form" id="layui-form" action="" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label layui-required-tip">通知名称：</label>
            <div class="layui-input-block">
                <input type="text" id="noticeName" name="noticeName" lay-verify="required" placeholder="通知名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-required-tip">通知内容：</label>
            <div class="layui-input-block">
                <textarea id="noticeContent" name="noticeContent" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否推送：</label>
            <div class="layui-input-block" id="isPush" >
                <input type="radio" name="isPush" value="1" title="是" >
                <input type="radio" name="isPush" value="0" title="否" checked>
            </div>
        </div>
        <div class="layui-form-item isUrl">
            <label class="layui-form-label">是否有链接：</label>
            <div class="layui-input-block" id="isUrl" >
                <input type="radio" name="isUrl" value="1" title="是" lay-filter="isUrl">
                <input type="radio" name="isUrl" value="0" title="否" checked lay-filter="isUrl">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布时间：</label>
            <div class="layui-input-block">
                <input type="radio" name="publishMode" value="1" lay-filter="pub-time" title="立即" checked="checked" />
                <input type="radio" name="publishMode" value="2" lay-filter="pub-time" title="定时" />
                <div id="publish-time" class="layui-inline">
                    <input class="layui-input" name="publishTime" id="publishTime" placeholder="默认采用当前时间"
                           onclick="layui.laydate.render({elem: this,min:0,type: 'datetime',show: true,closeStop: this, format: 'yyyy-MM-dd HH:mm:ss'})">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="save">立即提交</button>
                <a id="reset" class="layui-btn layui-btn-primary" >重置</a>
                <button type="button" class="layui-btn layui-btn-primary" onclick="javascript:history.back();" >返回</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script type="text/javascript" src="/js/global-config.js"></script>
<script>
    layui.use(['jquery','table','laydate','layer','form','util','layedit','upload','element'],function(){
        var $=layui.jquery,table=layui.table,laydate = layui.laydate,
            layer = layui.layer,form = layui.form,util=layui.util,
            layedit=layui.layedit,upload = layui.upload;

        //初始化kindeditor编辑器
        var keditor=initEditor("noticeContent");

        //时间选择器
        var start = {
            max: '2099-06-16 23:59:59',
            format: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetime',
            min:util.toDateString(new Date().toString(), 'yyyy-MM-dd HH:mm:ss')
        };

        $('#publishTime').on('click',function (){
            start.elem = this;
            laydate.render(start);
        });

        form.on('radio(pub-time)',function (data) {
            showHide("publish-time", data.value == "2");
        });

        showHide("publish-time",false);

        function showHide(id, show) {
            var str = "#"+id;
            if (show){
                $(str).show();
            }else{
                $(str).hide();
            }
            form.render();
        }

        form.on('radio(isUrl)',function (data) {
            $(".redirectUrl").remove();
            $(".relation").remove();
            if(data.value =="1"){
                $(".isUrl").after(
                    '<div class="layui-form-item relation"><label class="layui-form-label">关联位置：</label>' +
                    '<div class="layui-input-block">'+
                    '<input type="radio" id="relation_url" name="relation" lay-filter="select_relation" value="1" title="url" checked>'+
                    '<input type="radio" id="relation_native" name="relation" lay-filter="select_relation" value="2" title="native">' +
                    '</div></div>'
                );
                form.render();
                $(".relation").after(
                    '<div class="layui-form-item redirectUrl">'+
                    '<label class="layui-form-label layui-required-tip">URL：</label>'+
                    '<div class="layui-input-block">'+
                    '<input type="text" id="redirectUrl" name="redirectUrl" placeholder="URL" lay-verify="required" autocomplete="off" class="layui-input">'+
                    '</div></div>'
                );
            }else if(data.value =="0"){
                $("#relation_url").removeAttr("checked");
                $("#relation_native").removeAttr("checked");
                $(".relation").remove();
            }
            form.render();
        });

        form.on('radio(select_relation)',function (data) {
            $(".redirectUrl").remove();
            if(data.value =="1"){
                $(".relation").after(
                '<div class="layui-form-item redirectUrl">'+
                '<label class="layui-form-label layui-required-tip">URL：</label>'+
                '<div class="layui-input-block">'+
                '<input type="text" id="redirectUrl" name="redirectUrl" placeholder="URL" lay-verify="required" autocomplete="off" class="layui-input">'+
                '</div></div>'
                )
            }else if(data.value =="2"){
                $(".relation").after(
                    '<div class="layui-form-item redirectUrl">'+
                    '<label class="layui-form-label layui-required-tip">Native：</label>'+
                    '<div class="layui-input-block">'+
                    '<select id="redirectUrl" name="redirectUrl" lay-search placeholder="Native" lay-verify="required" autocomplete="off" class="layui-input"></select>'+
                    '</div></div>'
                )
                loadSelect();
            }
            form.render();
        });

        //重置
        $("#reset").on('click',function () {
            noticeName.value ='';
            keditor.html('');
            redirectUrl.value='';
        });

        //下拉框
        function loadSelect(){
            $.ajax({
                url:"/manage/notice/selreportlist",
                type:"post",
                dataType:"json",
                success:function(data){
                    var root = document.getElementById("redirectUrl");
                    for(var i=0;i<data.length;i++){
                        var option = document.createElement("option");
                        option.setAttribute("value",data[i]["id"]);
                        option.innerHTML = data[i]["appTitle"];
                        root.append(option);
                        form.render("select");
                    }
                }
            });
        }
        //表单提交
        form.on('submit(save)', function(data){
            if(keditor.count('text') == 0||keditor.count('html') == 0){
                layer.msg("请输入通知内容！", {icon: 5, shift: 6});
                keditor.focus();
                return false;
            }
            if(data.field.publishMode =="2" && data.field.publishTime == ""){
                layer.msg("请填写发布时间！", {icon: 5, shift: 6});
                return false;
            }
            layer.confirm('确认要提交吗？', {
                btn : [ '确定', '取消' ]//按钮
            }, function(index) {
                //同步编辑器内容到原来textarea
                keditor.sync('#noticeContent');
                data.field.noticeContent=keditor.html();
                $.ajax({
                    url:"/manage/notice/insaNotice",
                    type:"post",
                    data:data.field,
                    dataType:"json",
                    success:function (data) {
                        layer.alert(data.msg, {
                            skin: 'layui-layer-molv' //样式类名  自定义样式
                            ,closeBtn: 1    // 是否显示关闭按钮
                            ,anim: 1 //动画类型
                            ,btn: ['确定'] //按钮
                            ,icon: data.code>=0?1:2    // icon
                            ,yes:function(index){
                                if (data.code >= 0) {
                                    location.href = "/manage/notice/anotice";
                                }else{
                                    layer.close(index);
                                }
                            }
                        });
                    }
                })
            });
            return false;
        });
    });
</script>
</body>
</html>